package com.wwwarehouse.financialcenter.fragment.moneywarehouse.homepage;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.bean.carddesk.CardDeskMessageStableCardBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bindingcard.BindCardBean;
import com.wwwarehouse.financialcenter.bean.bindingcard.MoneyHouseInfomationBean;
import com.wwwarehouse.financialcenter.bean.bindingcard.PermissionBean;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.eventbus.CardInfoEvent;
import com.wwwarehouse.financialcenter.eventbus.RefreshEvent;
import com.wwwarehouse.financialcenter.fragment.base.TheParentFragment;
import com.wwwarehouse.financialcenter.fragment.moneywarehouse.bankcard.AddEnterpriseBankFragment;
import com.wwwarehouse.financialcenter.fragment.moneywarehouse.bankcard.BindBankCardListFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Description: 已有企业银行卡
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/11/13 10:35
 */
public class BankCardInfomationFragment extends TheParentFragment {

    ArrayList<Fragment> fragmentList;
    MyFragmentPagerAdapter adapter;
    private LinearLayout mCardList;
    private ElasticScrollView mElasticScroll;
    private CirclePageIndicator mPagerIndicator;
    private CustomViewPagerInternal mTaskPager;
    private String businessId;
    int mNum = 4;
    private AppCompatButton mAddbankCard;
    private ElasticScrollView elasticScrollView;
    public SlideBarView mSlideBarView;
    private CardDeskMessageStableCardBean.TasksBean tasksBean;
    private String cardUnitUkid;
    private Bundle bundle;
    private RelativeLayout mRelIndicator;
    private RelativeLayout mRelAddBank;

    @Override
    protected int getContentId() {
        return R.layout.financial_center_fragment_existing_bank_card;
    }

    @Override
    protected void init(View view) {
        mCardList = findView(view, R.id.ll_card_list);
        mElasticScroll = findView(view, R.id.elastic_scroll);
        mPagerIndicator = findView(view, R.id.pager_indicator);
        mTaskPager = findView(view, R.id.task_pager);
        mAddbankCard = findView(view, R.id.btn_add_bank_card);
        mSlideBarView = findView(view, R.id.sbv_slide);
        mRelIndicator = findView(view, R.id.rel_indcator);
        mRelAddBank = findView(view, R.id.rel_addbank);
        elasticScrollView = (ElasticScrollView) view.findViewById(R.id.elastic_scroll);
        EventBus.getDefault().register(this);
        bundle = getArguments();
        if (bundle != null) {
            MoneyHouseInfomationBean.DataBean data = (MoneyHouseInfomationBean.DataBean) bundle.getSerializable("moneyHomeData");
            tasksBean = bundle.getParcelable("tasksBean");
            if (data != null)
                businessId = data.getBusinessId() + "";
            if (tasksBean!=null){
                cardUnitUkid = tasksBean.getTaskTypeUkid();
            }
        }


        Map<String, Object> map = new HashMap<>();
        map.put("businessUnitId", businessId);
        map.put("cardUnitUkid",cardUnitUkid);
        httpPost(FinancialConstant.GETUNITSBYCARDUKID, map, 1, false, "");


    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BankCardInfomationFragment)
            mActivity.setTitle(getString(R.string.financial_center_existing_bankcard));
    }

    public void onEventMainThread(Object event) {
        if (event instanceof CardInfoEvent) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", ((CardInfoEvent) event).getListBean());
            bundle.putString("businessId", businessId);
            bundle.putString("cardUnitUkid", cardUnitUkid);
            CardInfoFragment cardInfoFragment = new CardInfoFragment();
            cardInfoFragment.setArguments(bundle);
            pushFragment(cardInfoFragment);
        } else if (event instanceof RefreshEvent) {
            loadDatas();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void loadDatas() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> query = new HashMap<>();
        map.put("businessId", businessId);
        query.put("page", 1);
        query.put("size", mNum);
        map.put("query", query);
        httpPost(FinancialConstant.GETS_BINDING_BANKCARD, map, 0, false, "");

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what == 0) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                BindCardBean.DataBean dataBeen = JSON.parseObject(commonClass.getData().toString(), BindCardBean.DataBean.class);
                if (dataBeen.getTotal() == 0) {
                    showEmptyViewpager();
                } else {
                    showViewpager(dataBeen.getTotal());
                }
            }  else {
                toast(commonClass.getMsg());
            }
        }else if (what==1){
            if (TextUtils.equals("0", commonClass.getCode())) {
                try {
                    List<PermissionBean.DataBean> dataBeen = JSON.parseArray(commonClass.getData().toString(), PermissionBean.DataBean.class);

                    String clientShow = dataBeen.get(0).getClientShow();

                    if ("1".equals(clientShow)){
                        elasticScrollView.setOnPullListener(new ElasticScrollView.OnPullListener() {
                            @Override
                            public void onDownPull() {

                            }

                            @Override
                            public void onUpPull() {
                                mAddbankCard.setVisibility(mAddbankCard.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                                mRelIndicator.setVisibility(mAddbankCard.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                            }
                        });

                        mAddbankCard.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                AddEnterpriseBankFragment addEnterpriseBankFragment = new AddEnterpriseBankFragment();
                                addEnterpriseBankFragment.setArguments(bundle);
                                pushFragment(addEnterpriseBankFragment);
                            }
                        });
                    } else {
                        mAddbankCard.setVisibility(View.GONE);
                        mRelIndicator.setVisibility(View.VISIBLE);
                    }
                }  catch (Exception e){
                    mAddbankCard.setVisibility(View.GONE);
                    mRelIndicator.setVisibility(View.VISIBLE);
                }

            } else {
                mAddbankCard.setVisibility(View.GONE);
                mRelIndicator.setVisibility(View.VISIBLE);
            }
        }
    }

    private void showEmptyViewpager() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new EmptyFragment());
        if (BankCardInfomationFragment.this.isAdded()) {
            adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
            mTaskPager.setAdapter(adapter);
            mPagerIndicator.setVisibility(View.GONE);
        }
    }

    @Override
    public void onReLoad(int code) {
        loadDatas();
    }

    private void showViewpager(int total) {
        fragmentList = new ArrayList<>();
        int count = 0;
        if (total > mNum) {
            if ((total - mNum) % mNum == 0) {
                count = (total - mNum) / mNum;
            } else {
                count = (total - mNum) / mNum + 1; // 总共显示多少张卡片
            }
        } else {
            count = 0;
        }

        for (int i = 0; i <= count; i++) {
            BindBankCardListFragment bindBankCardListFragment = new BindBankCardListFragment();
            Bundle bundle = new Bundle();
            bundle.putString("businessId", businessId);
            bundle.putInt("pos", i);
            bundle.putInt("num", mNum);
            bindBankCardListFragment.setArguments(bundle);
            fragmentList.add(bindBankCardListFragment);

        }
        if (BankCardInfomationFragment.this.isAdded()) {
            adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
            mTaskPager.setAdapter(adapter);
        }

        if (count <= 6) {
            if (count > 0) {
                mPagerIndicator.setVisibility(View.VISIBLE);
                mSlideBarView.setVisibility(View.GONE);
                mPagerIndicator.setViewPager(mTaskPager, 0);
            } else {
                mPagerIndicator.setVisibility(View.GONE);
                mSlideBarView.setVisibility(View.GONE);
            }
        } else {
            mPagerIndicator.setVisibility(View.GONE);
            mSlideBarView.setVisibility(View.VISIBLE);
            mSlideBarView.setTotalPage(count);
            mSlideBarView.setOnSlideChange(new SlideBarView.OnSlideChange() {
                @Override
                public void onSlideChange(int page) {
                    mTaskPager.setCurrentItem(page, false);
                }
            });
            if (mTaskPager.getAdapter()!=null){
                mSlideBarView.setViewPagerInternal(mTaskPager);
            }
        }
    }


}
